package com.ttbj.controller.truck;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.company.CompanyService;
import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.SalesmanDto;
import com.ttbj.api.dto.truck.TruckCustomDto;
import com.ttbj.api.dto.truck.TruckDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.truck.TruckService;
import com.ttbj.constant.Const;
import com.ttbj.constant.annotation.SystemControllerLog;
import com.ttbj.util.ViewDataUtil;

@Controller
public class TruckController {
	
	@Autowired
	private TruckService truckService;
	@Autowired
	private CompanyService companyService;
	
	
	/**
	 * 货车列表页面
	 * @return
	 */
	@RequestMapping(value="/truck/truck_list.htm")
	public String truck_list() {
		return "truck/truck_list";
	}
	
	/**
	 * 编辑和增加货车页面
	 * @return
	 */
	@RequestMapping(value="/truck/edit_truck.htm")
	public String edit_truck() {
		return "truck/edit_truck";
	}
	
	/**
	 * 查看车辆位置页面
	 * @return
	 */
	@RequestMapping(value="/truck/truck_position.htm")
	public String truck_position() {
		return "truck/truck_position";
	}
	
	/**
	 * 货车列表信息
	 * @param request
	 * @param truckCustomDto 货车信息
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/truck/getTruckInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getTruckInfoByPage(HttpServletRequest request, 
								@RequestParam(required=false) String truckCarNum, 
								@RequestParam(required=false) String driverName, Page page) {
		try {
			//获取当前登录的用户信息
			SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
			//通过公司编号获取公司Id
			Long companyId = this.companyService.findCompanyIdByNum(salesmanDto.getSalesmanComnum());
			TruckCustomDto truckCustomDto = new TruckCustomDto();
			truckCustomDto.setTruckCarnum(truckCarNum);
			truckCustomDto.setDriverRealName(driverName);
			truckCustomDto.setTruckCompany(companyId);
			List<TruckCustomDto> truckCustomDtos = this.truckService.findTruckInfoByPage(truckCustomDto, page);
			int count = 0;
			if (null != truckCustomDtos) {
				count = this.truckService.findTruckInfoByPageCount(truckCustomDto);
			}
			return ViewDataUtil.buildSuccessPageJson("货车列表信息", truckCustomDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 增加货车信息
	 * @param request
	 * @param truckDto 货车信息
	 * @return
	 */
	@RequestMapping(value="/truck/addToTruckInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="增加货车信息")
	public String addToTruckInfo(HttpServletRequest request, TruckDto truckDto) {
		try {
			//获取当前主体
			Subject subject = SecurityUtils.getSubject();
			boolean isPer = subject.isPermitted("truck:edit");
			//获取主体有无truck:edit（货车新增、编辑）资源
			if (isPer) {	//如果有这个资源
				boolean isRole = subject.hasRole("root");	//获取是否为root角色
				if (!isRole) {	//如果不是root角色
					//获取当前登录的用户信息
					SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
					//通过公司编号获取公司Id
					Long companyId = this.companyService.findCompanyIdByNum(salesmanDto.getSalesmanComnum());
					truckDto.setTruckCompany(companyId);
				}
				this.truckService.addTruckInfo(truckDto);
			}
			return ViewDataUtil.buildSuccessJson("增加成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 通过id查询货车部分信息，用于编辑显示信息
	 * @param request
	 * @param truckId 货车id
	 * @return
	 */
	@RequestMapping(value="/truck/getTruckInfoUsedEdit.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getTruckInfoUsedEdit(HttpServletRequest request, @RequestParam Long truckId) {
		TruckDto truckDto = this.truckService.findTruckInfoUsedEdit(truckId);
		return ViewDataUtil.buildSuccessJson("货车信息", truckDto);
	}
	
	/**
	 * 编辑货车信息
	 * @param request
	 * @param truckDto
	 * @return
	 */
	@RequestMapping(value="/truck/modifyTruckInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改货车信息")
	public String modifyTruckInfo(TruckDto truckDto) {
		try {
			this.truckService.editTruckInfo(truckDto);
			return ViewDataUtil.buildSuccessJson("修改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 更改货车状态，禁用和启用
	 * @param request
	 * @param truckId 货车id
	 * @param isDel 禁用和启用标识：0禁用，1未禁用
	 * @return
	 */
	@RequestMapping(value="/truck/modifyTruckIsDel.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="禁用或启用货车状态")
	public String modifyTruckDisable(@RequestParam Long truckId, @RequestParam Integer isDel) {
		try {
			this.truckService.editTruckDisable(isDel, truckId);
			return ViewDataUtil.buildSuccessJson("修改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 通过货车id查询车牌号和entity，用于百度地图显示
	 * @param request
	 * @param truckIds 货车主键id数组
	 * @return
	 */
	@RequestMapping(value="/truck/getTruckCarnumAndEntity.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getTruckCarnumAndEntity(HttpServletRequest request, @RequestParam(value="truckIds") Long[] truckIds) {
		try {
			List<TruckCustomDto> truckCustomDtos = this.truckService.findTruckCarnumAndEntity(truckIds);
			return ViewDataUtil.buildSuccessJson("查询成功", truckCustomDtos);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
